package com.example.administrator.capital.model;
import com.example.administrator.capital.R;
import com.example.administrator.capital.model.base.BaseModel;
import com.example.administrator.capital.model.base.IActionRequest;
import com.example.administrator.capital.model.bean.ErrorBean;
import com.example.administrator.capital.url.Url;
import com.example.administrator.capital.utils.JsonUtils;
import com.example.administrator.capital.utils.LogUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

/**
 * Created by Administrator on 2017/12/1 0001.
 * model层（处理业务和逻辑）
 */
public class LoginModel extends BaseModel {
    /**
     * @param iActionRequest
     * @param username
     * @param password
     * @param yzm
     * @param ivuid
     * 注册
     */
    public void register(final IActionRequest<String> iActionRequest,String username, String password, String yzm,String ivuid ){
        OkGo.<String>post(Url.REGISTER)
                .params("password", password)
                .params("yzm",yzm)
                .params("tel",username)
                .params("YZCode",ivuid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.d("注册"+response.body());
                        if (response.body().contains("\"code\":1")) {
                            iActionRequest.success(response);
                        } else {
                            ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);
                            iActionRequest.failMsg(bean.getMsg());
                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        iActionRequest.code(R.string.error_msg);}});
    }
    /**
     * @param iActionRequest
     * @param username 账号
     * @param password 密码
     *登录
     */
    public void getSign(final IActionRequest<String> iActionRequest, String username, String password ){
        OkGo.<String>post(Url.LOGIN)
                .params("username",username)
                .params("password",password)
                .execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                LogUtils.d("登录"+response.body());
                if (response.body().contains("\"code\":1")) {
                    iActionRequest.success(response);
                } else {
                    ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);
                    iActionRequest.failMsg(bean.getMsg());
                }
            }
            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                iActionRequest.code(R.string.error_msg);}});
    }
    /**
     * @param iActionRequest
     * @param mobile
     *
     */
    public void getYZM(final IActionRequest<String> iActionRequest, String mobile ){
        OkGo.<String>post(Url.GETYZM)
                .params("mobile",mobile)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.d("获取验证码"+response.body());
                        if (response.body().contains("\"code\":1")) {
                            iActionRequest.success(response);
                        } else {
                            ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);
                            iActionRequest.failMsg(bean.getMsg());
                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        iActionRequest.code(R.string.error_msg);
                    }
                });
    }
}
